<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-08-02
 * Time: 下午 3:12
 */
namespace app\api\controller;
use think\Controller;
use think\Db;
class Member extends Controller{
    //检测会员是不是第一次登录
    public function checkMemberFirst($opendid){
        $data = Db::name('member')->where('opend_id',$opendid)->find();
        if(empty($data)){
            return true;
        }else{
            return false;
        }
    }
    //会员插入
    public function addMember($data){
        $res = Db::name('member')->insertGetId($data);;
        if($res){
           return $res;
        }else{
            return false;
        }
    }
    //查询会员信息（根据会员ID）
    public function getMemberInfo($member_id){
        $data = Db::name('member')->where('member_id',$member_id)->find();
        if(!empty($data)){
            if($data['custom']){
                $arr = json_decode($data['custom'],true);
                foreach($arr as $k=>$v){
                    $data[$k] = $v;
                }
            }
        }
        return $data;
    }
    //查询会员信息（根据会员opendid）
    public function getMemberInfoOpendid($opend){
        $data = Db::name('member')->where('opend_id',$opend)->find();
        if(!empty($data)){
            if($data['custom']){
                $arr = json_decode($data['custom'],true);
                foreach($arr as $k=>$v){
                    $data[$k] = $v;
                }
            }
        }
        return $data;
    }
    //修改会员资料
    public function saveMemberInfo($member_id,$data){
        $res = Db::name('member')->where('member_id',$member_id)->update($data);
        if($res === false){
            return false;
        }else{
            return true;
        }
    }
    //会员收藏资源
    public function memberCollect($data){
        $res = Db::name('collect')->insert($data);
        if(!$res){
            return false;
        }else{
            return true;
        }
    }
    //会员取消收藏资源
    public function deleteMemberCollect($member_id,$source_id){
        $res = Db::name('collect')->where('member_id',$member_id)->where('source_id',$source_id)->delete();
        if(!$res){
            return false;
        }else{
            return true;
        }
    }
    //会员收藏资源列表
    public function memberCollectList($member_id,$page,$count,$type){
        $map = array();
        if($type != 3){
            $map['snake_source.type'] = $type;
        }
        $data = Db::name('collect')->join('source','snake_source.source_id = snake_collect.source_id')->where('snake_collect.member_id',$member_id)->where($map)->limit($count)->page($page)->select();
        return $data;
    }
    //会员上传群资源
    public function memberUploadQunSource($data){
        $res = Db::name('qun_source')->insert($data);
        if(!$res){
            return false;
        }else{
            return true;
        }
    }
    //会员取消上传群资源
    public function memberDeleteQunSource($source_id,$member_id,$qun_id){
        $res = Db::name('qun_source')->where('source_id',$source_id)->where('member_id',$member_id)->where('qun_id',$qun_id)->delete();
        if(!$res){
            return false;
        }else{
            return true;
        }
    }
    //添加好友
    public function addFriends($data){
        $res = Db::name('friends')->insert($data);
        if($res){
            return true;
        }else{
            return false;
        }
    }
    //发送消息
    public function sendMsg($data){
        $res = Db::name('msg')->insert($data);
        if($res){
            return true;
        }else{
            return false;
        }
    }
    //好友列表
    public function getFriendsList($page,$count,$member_id){
        $data = Db::name('friends')
            ->join('member','snake_member.member_id = snake_friends.member_id2')
            ->where('snake_friends.member_id1',$member_id)
            ->limit($count)
            ->page($page)
            ->order('snake_friends.is_zd1 desc,snake_friends.time1 desc')
            ->select();
        if(empty($data)){
            $data = Db::name('friends')
                ->join('member','snake_member.member_id = snake_friends.member_id1')
                ->where('snake_friends.member_id2',$member_id)
                ->limit($count)
                ->page($page)
                ->order('snake_friends.is_zd2 desc,snake_friends.time2 desc')
                ->select();
        }
        return $data;
    }
    //好友列表
    public function getFriendsListInfo($member_id,$keyword){
        $data = Db::name('friends')
            ->join('member','snake_member.member_id = snake_friends.member_id2')
            ->where('snake_friends.member_id1',$member_id)
            ->where('snake_member.name like "%'.$keyword.'%"')
            ->order('snake_friends.is_zd1 desc,snake_friends.time1 desc')
            ->select();
        if(empty($data)){
            $data = Db::name('friends')
                ->join('member','snake_member.member_id = snake_friends.member_id1')
                ->where('snake_friends.member_id2',$member_id)
                ->where('snake_member.name like "%'.$keyword.'%"')
                ->order('snake_friends.is_zd2 desc,snake_friends.time2 desc')
                ->select();
        }
        return $data;
    }
    //好友列表
    public function getAllFriendsList($member_id){
        $data = Db::name('friends')
            ->join('member','snake_member.member_id = snake_friends.member_id2')
            ->where('snake_friends.member_id1',$member_id)
            ->order('snake_friends.is_zd1 desc,snake_friends.time1 desc')
            ->select();
        if(empty($data)){
            $data = Db::name('friends')
                ->join('member','snake_member.member_id = snake_friends.member_id1')
                ->where('snake_friends.member_id2',$member_id)
                ->order('snake_friends.is_zd2 desc,snake_friends.time2 desc')
                ->select();
        }
        return $data;
    }
    //会员群列表
    public function getMemberQun($page,$count,$member_id){
        return Db::name('qun_member')
            ->join('qun','snake_qun.qun_id = snake_qun_member.qun_id')
            ->where('snake_qun_member.member_id',$member_id)
            ->limit($count)
            ->page($page)
            ->order('snake_qun_member.is_zd desc,snake_qun_member.zd_time desc')
            ->select();
    }
    //会员群列表
    public function getMemberQunInfo($member_id){
        return Db::name('qun_member')
            ->join('qun','snake_qun.qun_id = snake_qun_member.qun_id')
            ->where('snake_qun_member.member_id',$member_id)
            ->order('snake_qun_member.is_zd desc,snake_qun_member.zd_time desc')
            ->select();
    }
    //查询备注
    public function getMemberRemarks($member_id,$friends_id){
        $info = Db::name('friends')->where('member_id1',$member_id)->where('member_id2',$friends_id)->find();
        if(empty($info)){
            $data = Db::name('friends')->where('member_id2',$member_id)->where('member_id1',$friends_id)->find();
        }
        if(!empty($data)){
            $remarks = json_decode($data['member_id2_name'],true);
        }
        if(!empty($info)){
            $remarks = json_decode($info['member_id1_name'],true);
        }
        return $remarks;
    }
    //修改好友备注
    public function saveMemberRemarks($member_id,$friends_id,$remarks){
        $info = Db::name('friends')->where('member_id1',$member_id)->where('member_id2',$friends_id)->find();
        if($info){
            $res = Db::name('friends')->where('member_id1',$member_id)->where('member_id2',$friends_id)->update(array('member_id1_name'=>$remarks));
        }else{
            $data = Db::name('friends')->where('member_id2',$member_id)->where('member_id1',$friends_id)->find();
            if($data){
                $res = Db::name('friends')->where('member_id2',$member_id)->where('member_id1',$friends_id)->update(array('member_id2_name'=>$remarks));
            }else{
                return false;
            }
        }
        if($res === false){
            return false;
        }else{
            return true;
        }
    }
    //好友置顶
    public function setMemberIsZd($member_id,$friends_id,$is_zd){
        $info = Db::name('friends')->where('member_id1',$member_id)->where('member_id2',$friends_id)->find();
        if($info){
            $res = Db::name('friends')->where('member_id1',$member_id)->where('member_id2',$friends_id)->update(array('is_zd1'=>$is_zd));
        }else{
            $data = Db::name('friends')->where('member_id2',$member_id)->where('member_id1',$friends_id)->find();
            if($data){
                $res = Db::name('friends')->where('member_id2',$member_id)->where('member_id1',$friends_id)->update(array('is_zd2'=>$is_zd));
            }else{
                return false;
            }
        }
        if($res === false){
            return false;
        }else{
            return true;
        }
    }
    //检查是否是群主，管理员
    public function getMemberRole($member_id,$qun_id){
        $data = Db::name('qun_member')->where('member_id',$member_id)->where('qun_id',$qun_id)->find();
        if(empty($data)){
            return 0;//不是群成员
        }
        if($data['is_master'] == 1){
            return 1;//是群主
        }
        if($data['is_head'] == 1){
            return 2;//管理员
        }
        return 3;//群成员
    }
    //设置管理员
    public function setQunMaster($qun_id,$member_id,$is_head){
        $res = Db::name('qun_member')->where('qun_id',$qun_id)->where('member_id',$member_id)->update(array('is_head'=>$is_head));
        if($res === false){
            return false;
        }else{
            return true;
        }
    }
    //剔除群成员
    public function deleteQunMember($qun_id,$member_id){
        $res = Db::name('qun_member')->where('qun_id',$qun_id)->where('member_id',$member_id)->delete();
        if(!$res){
            return false;
        }else{
            return true;
        }
    }
    //检测两个人是不是好友
    public function getMemberMyFriend($member_id1,$member_id2){
        $info = Db::name('friends')->where('member_id1',$member_id1)->where('member_id2',$member_id2)->find();
        if(!empty($info)){
            return true;
        }
        $data = Db::name('friends')->where('member_id1',$member_id2)->where('member_id2',$member_id1)->find();
        if(!empty($data)){
            return true;
        }
        return false;
    }
    //添加我的名片
    public function addMyCard($data){
        $res = Db::name('card')->insert($data);
        if($res){
            return true;
        }else{
            return false;
        }
    }
    //查询我的名片
    public function getMemberCard($member_id){
        return  Db::name('card')->where('member_id',$member_id)->select();
    }
    //意见反馈
    public function feedBack($data){
        $res = Db::name('feedback')->insert($data);
        if($res){
            return true;
        }else{
            return false;
        }
    }
    //举报信息
    public function memberReport($data){
        $res = Db::name('report')->insert($data);
        if($res){
            return true;
        }else{
            return false;
        }
    }
    //判断是不是自己收藏自己的资源
    public function checkMyCollectMySource($member_id,$source_id){
        $data = Db::name('source')->where('member_id',$member_id)->where('source_id',$source_id)->find();
        if($data){
            return 1;
        }else{
            return 0;
        }
    }
}